var app = getApp();
var c = require("../libs/common.js");
var config = require("../libs/config.js");
var upimg = require('../libs/upimg.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    widthinfo: '100rpx',
    date: '2016-09-01',
    sex: ['男', '女'],
    index: 0,
    region: ['北京市', '北京市', '东城区']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log('userinfo');
    c.loginVer();
    c.uinfo(this);
    var index = 0;
    if (this.data.uinfo.sex == 'n') {
      index = 0;
    } else {
      index = 1;
    }
    console.log(this.data);
    this.setData({
      head: this.data.uinfo.head,
      index: index,
      birthday: this.data.uinfo.birthday,
      region: this.data.uinfo.address
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成1
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //姓名
  blurname: function(e) {
    console.log(e.detail.value);
    this.upInfoApi('name', e.detail.value);
  },
  //性别
  bindPickerChange: function(e) {
    var sex = e.detail.value;
    if (sex == 0) {
      var sexinfo = 'n';
    } else {
      var sexinfo = 'v';
    }
    this.upInfoApi('sex', sexinfo);
    this.setData({
      index: sex
    });
  },
  //日期
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为2', e.detail.value)
    var birthday = e.detail.value;
    this.setData({
      "birthday": birthday
    })
    this.upInfoApi('birthday', e.detail.value);
  },
  //地区
  bindRegionChange: function(e) {
    var address = e.detail.value;
    var value = address.join("-");
    this.setData({
      region: e.detail.value
    })
    this.upInfoApi('address', value);
  },
  //修改api
  upInfoApi(type, datainfo) {
    var uid = this.data.uinfo.id;
    wx.showLoading({
      title: '加载中...',
    })
    c.ajax(
      config.hostapi + 'MyInfo/upInfo', {
        uid: uid,
        type: type,
        data: datainfo
      },
      res => {
        console.log(res);
        if (res.data.code == 100) {
          wx.setStorageSync('userinfo', res.data.data);
          wx.hideLoading();
          wx.showToast({
            title: '修改成功',
            image: '/images/dui.png'
          })
        } else {
          wx.showToast({
            title: res.data.info,
            icon: 'none'
          })
          wx.hideLoading();
          return;
        };
      }
    )
  },
  simpleUpload: function(e) {
    var path = 'user/' + new Date().getTime() + '.png';
    var that = this;
    var uid = app.u.user_id;
    upimg.simpleUpload(
      path,
      function(res) {
        var image = res[0]['dbs'];
        c.ajax(
          app.url + 'setmyinfo', {
            type: 1,
            value: '/' + path,
            uid: uid
          },
          res => {
            setTimeout(function() {
              that.setData({
                image: image
              });
            }, 1000)
            if (res.data == 1) {
              wx.showToast({
                title: '修改成功',
                icon: 'none'
              })
            }
          }
        )
        that.setData({
          arrayimg: res
        })
      },
      6
    );
  },
  clickfuzhi: function(res) {
    var info = this.data;
    var phone = info.data.phone;
    var stringinfo = 'http://www.wyueke.com?intivi=' + phone;
    wx.setClipboardData({
      data: stringinfo,
      success: function(res) {
        wx.getClipboardData({
          success: function(res) {
            wx.showToast({
              title: '复制成功',
              icon: 'success',
              duration: 2000
            })
          }
        })
      }
    })
  },
  imgtap: function() {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      success: (res) => {
        wx.showLoading({
          title: '上传中...',
          mask: 'true'
        })
        var i = 0; //第几个
        var img_num = res.tempFiles.length;
        this.uploadDIY(res.tempFilePaths, i, img_num);
      },
    });
  },
  uploadDIY(filePaths, i, img_num) {
    var that = this;
    var img_num = img_num;
    wx.uploadFile({
      url: config.hostapi + 'UpFile/FileUpImg',
      filePath: filePaths[i],
      name: 'fileData',
      pathname: 'sendpet',
      success: (res) => {
        var data = JSON.parse(res.data.replace(/^\s+|\s+$/g, ""));
        var head = data.data;
        that.upInfoApi('head', head);
        that.setData({
          head: head
        });
      }
    });
  },
  submit: function() {

    wx.showLoading({
      title: '提交中...',
      mask: 'true'
    })
  }
})